package cn.walls1717.wallsmall.product.service.impl;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.product.dao.CategoryBrandRelationDao;
import cn.walls1717.wallsmall.product.entity.BrandEntity;
import cn.walls1717.wallsmall.product.entity.CategoryBrandRelationEntity;
import cn.walls1717.wallsmall.product.entity.CategoryEntity;
import cn.walls1717.wallsmall.product.service.BrandService;
import cn.walls1717.wallsmall.product.service.CategoryBrandRelationService;
import cn.walls1717.wallsmall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity>
    implements CategoryBrandRelationService {

    @Resource
    private BrandService brandService;
    @Resource
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(new Query<CategoryBrandRelationEntity>().getPage(params),
            new QueryWrapper<CategoryBrandRelationEntity>());

        return new PageUtils(page);
    }

    /**
     * 保存品牌与分类关系的详细信息
     *
     * @param categoryBrandRelation 品牌与分类关联关系参数
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brand = brandService.getById(brandId);
        CategoryEntity category = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        save(categoryBrandRelation);
    }

    /**
     * 使用品牌id更新品牌名
     *
     * @param brandId 品牌id
     * @param brandName 品牌名
     */
    @Override
    public void updateBrandNameByBrandId(Long brandId, String brandName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(brandName);
        update(categoryBrandRelationEntity,
            lambdaUpdate().eq(CategoryBrandRelationEntity::getBrandId, brandId).getWrapper());
    }

    /**
     * 更新分类名
     *
     * @param catId 分类id
     * @param categoryName 分类名
     */
    @Override
    public void updateCategoryByCatId(Long catId, String categoryName) {
        baseMapper.updateCategoryByCatId(catId, categoryName);
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId 分类id
     * @return 品牌数据
     */
    @Override
    public List<CategoryBrandRelationEntity> listBrandsByCatId(Long catId) {
        return lambdaQuery().eq(CategoryBrandRelationEntity::getCatelogId, catId).list();
    }
}